package domain.entity.sevice;

import domain.entity.Dept;
import dto.DeptDto;
import vo.DeptVo;
import vo.Resut;

import java.util.Arrays;

/**
 * @author 周志豪
 * @since 2025/4/20 14:53
 */
public class DeptService {
    public static int DATA_SIZE = 100;
    public static Dept[] data = new Dept[100];
    public static int idCounter = 1;

    public static DeptVo converToVo(Dept dept) {
        return new DeptVo(dept.id, dept.name, dept.phone);
    }

    public static Resut list() { // 修改返回类型为 Resut
        DeptVo[] temp = new DeptVo[idCounter]; // 动态调整数组大小
        int length = 0;

        for (int i = 1; i < idCounter; i++) { // 遍历有效数据
            if (data[i] != null) { // 检查当前索引是否有数据
                temp[length++] = converToVo(data[i]);
            }
        }

        // 使用 Resut.success 方法封装结果
        return Resut.success("查询成功", Arrays.copyOf(temp, length));
    }

    public static Resut create(DeptDto dto) {
        if (idCounter > DATA_SIZE) {
            return Resut.failuers("创建失败，超出储存容量");
        }

        Dept dept = new Dept(dto.name, dto.phone);
        dept.id = idCounter++;
        data[dept.id] = dept;

        return Resut.success("创建成功", null);
    }
}
